-- exchange_pet
-- Created by chengb Sep/11/2015
-- 成就：兑换冈布奥

-- 内部函数声明
local whenExchangePet;

-- 注册完成兑换冈布奥的回调
EventMgr.register("game_achievement/exchange_pet", event.EXCHANGE_PET, function(args)
    whenExchangePet(args);
end);

-- 注册完成兑换冈布奥的回调
whenExchangePet = function(classId)
    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == GameAchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        local config = GameAchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            if type(ruleArg["class_id"]) == 'number' and
               ruleArg["class_id"] ~= classId then
                -- 不是需求的兑换冈布奥
                return false;
            end
        end

        return true;
    end

    -- 遍历所有的子规则
    local ids = GameAchievementM.getIdsByRule("exchange_pet");
    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            GameAchievementM.tryComplete(id, 1);
        end
    end
end

return {
    -- 获取成就描述
    queryDesc = function(id)
        local config = GameAchievementM.query(id);

        local ruleArg  = config["rule_arg"];
        local progress = config["progress"];
        local desc     = config["desc"];

        desc = string.gsub(desc, "{0}", progress);
        return desc;
    end,
};